<?php

if (!function_exists('millisecond')) {
  function millisecond()
  {
    return ceil(microtime(true) * 1000);
  }
}

if (!function_exists('encrypt_password')) {
  function encrypt_password($password, $salt)
  {
    return md5(md5($password) . $salt);
  }
}

// 获取毫秒时间戳
if (!function_exists('get_timestamp')) {
  function get_timestamp($time)
  {
    if (is_numeric($time)) {
      return str_pad($time, 13, "0", STR_PAD_RIGHT);
    } else {
      return strtotime($time) * 1000;
    }
  }
}

// 从时间戳获取日期
if (!function_exists('get_date')) {
  function get_date($timestamp, $format = 'Y-m-d')
  {
    if (is_numeric($timestamp)) {
      return date($format, substr($timestamp, 0, 10));
    } else {
      return $timestamp;
    }
  }
}


/**
 * 页面json 输出
 * @param int $code
 * @param $msg
 * @param $json
 */
if (!function_exists('resJson')) {
  function resJson($code, $data = null, $msg = null)
  {
    if ($code !== 0 && !empty($data) && $msg === null) {
      //只有2个参数且code不为0时，代表返回code码和错误信息
      $msg = $data;
      $data = null;
    } else {
      if ($msg === null) {
        $msg = App\Http\Controllers\Controller::getMsg($code);
      }
    }
    return response()->json([
      'code' => $code,
      'msg' => $msg,
      'current_time' => millisecond(),
      'data' => $data,
    ]);
  }
}
